import 'dart:async';

import 'package:chat_client/models/session_model.dart';
import 'package:chat_client/widgets/custom_global_widgets.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';

class VoiceCallScreen extends StatefulWidget {
  final SessionModel session;

  const VoiceCallScreen({super.key, required this.session});

  @override
  _VoiceCallScreenState createState() => _VoiceCallScreenState();
}

class _VoiceCallScreenState extends State<VoiceCallScreen>
    with SingleTickerProviderStateMixin {
  late AnimationController _controller;
  bool _isMuted = false;
  bool _isSpeakerOn = true;
  Duration _callDuration = Duration.zero;
  bool _isConnected = false; // 模拟连接状态

  @override
  void initState() {
    super.initState();
    _controller = AnimationController(
      vsync: this,
      duration: const Duration(seconds: 2),
    )..repeat();

    // 模拟通话计时
    SchedulerBinding.instance.addPostFrameCallback((_) {
      Timer.periodic(const Duration(seconds: 1), (timer) {
        setState(() => _callDuration += const Duration(seconds: 1));
      });
    });
  }

  @override
  void dispose() {
    _controller.dispose();
    super.dispose();
  }

  Widget _buildCallStatus() {
    return Column(
      children: [
        Text(
          _isConnected ? '语音通话中' : '等待接听…',
          style: const TextStyle(
            color: Colors.white,
            fontSize: 18,
            fontWeight: FontWeight.w500,
          ),
        ),
        const SizedBox(height: 8),
        Text(
          '${_callDuration.inMinutes.toString().padLeft(2, '0')}:'
          '${(_callDuration.inSeconds % 60).toString().padLeft(2, '0')}',
          style: const TextStyle(
            color: Colors.white70,
            fontSize: 16,
          ),
        ),
      ],
    );
  }

  Widget _buildActionButton(IconData icon, String label,
      {Color? color, VoidCallback? onPressed}) {
    return Column(
      children: [
        Container(
          decoration: BoxDecoration(
            color: Colors.black.withOpacity(0.3),
            shape: BoxShape.circle,
          ),
          child: IconButton(
            icon: Icon(icon, size: 28),
            color: color ?? Colors.white,
            onPressed: onPressed,
          ),
        ),
        const SizedBox(height: 8),
        Text(
          label,
          style: const TextStyle(
            color: Colors.white70,
            fontSize: 12,
          ),
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.grey[850],
      body: Stack(
        children: [
          // 头像背景
          Center(
            child: AnimatedBuilder(
              animation: _controller,
              builder: (_, child) {
                return Container(
                  width: 200 + (_controller.value * 50),
                  height: 200 + (_controller.value * 50),
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    color: Colors.black.withOpacity(0.1),
                  ),
                );
              },
            ),
          ),

          Column(
            children: [
              const Spacer(flex: 2),
              // 用户头像
              Container(
                  width: 120,
                  height: 120,
                  decoration: BoxDecoration(
                    shape: BoxShape.circle,
                    border: Border.all(
                      color: Colors.white30,
                      width: 2,
                    ),
                  ),
                  child: SquareAvatar(
                    imageSource: '',
                  )),
              const SizedBox(height: 32),
              _buildCallStatus(),
              const Spacer(flex: 3),
              // 操作按钮区
              Padding(
                padding: const EdgeInsets.symmetric(horizontal: 32),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: [
                    _buildActionButton(
                      _isMuted ? Icons.mic_off : Icons.mic,
                      _isMuted ? '取消静音' : '静音',
                      onPressed: () => setState(() => _isMuted = !_isMuted),
                    ),
                    _buildActionButton(
                      _isSpeakerOn ? Icons.volume_up : Icons.volume_off,
                      _isSpeakerOn ? '扬声器' : '听筒',
                      color: _isSpeakerOn ? Colors.green : Colors.grey,
                      onPressed: () =>
                          setState(() => _isSpeakerOn = !_isSpeakerOn),
                    ),
                    _buildActionButton(
                      Icons.call_end,
                      '挂断',
                      color: Colors.red,
                      onPressed: () => Navigator.pop(context),
                    ),
                  ],
                ),
              ),
              const SizedBox(height: 48),
            ],
          ),
        ],
      ),
    );
  }
}
